// pages/select/select.js
const app = getApp();
const QQMapWX = require("../../utils/qqmap-wx-jssdk.min.js");
Page({

  data: {
    flag: false,
    cities: [],
    loading: true,
    hotlist: [{ code: "100000", city: "北京市" }, { code: 200000, city: "上海市" }, { code: 524200, city: "广东省" }, { code: 215200, city: "江苏省" }, { code: 311800, city: "浙江省" }, { code: 455000, city: "河南省" }, { code: 614000, city: "四川省" }, { code: 421000, city: "湖南省" }, { code: 535000, city: "广西省" }, { code: 363000, city: "福建省" }, { code: 272100, city: "山东省" }, { code: 338000, city: "江西省" }, { code: "030000", city: "山西省" }, { code: "053000", city: "河北省" }],
    toView: 'A',
    card: '',
    cardflag: false,
    windowHeight: '',
    words: [],
    scrollflag: true,
    hint: false,
    addbtn: false,
    icon: false,
    cueflag: false,
    homeUrl: "https://shuigongbao.witlending.com/img/home/",
    allMsg: {}
  },
  scrollmp() {

  },
  /**
   * 生命周期函数--监听页面加载
   */
  openSetting: function (e) {
    if (e.detail.authSetting['scope.userLocation']) {
      this.position()
    } else {

    }
  },
  newpalce: function () {
    wx.setStorageSync("allMsg", this.data.allMsg)
    if (this.data.payfor) {
      wx.navigateTo({
        url: `../providentfund/providentfund?payfor=true&orderid=${this.data.orderid}`,
      })
    } else {
      wx.redirectTo({
        url: '../providentfund/providentfund',
      })
    }

  },
  //获取位置
  position: function () {
    let that = this;
    let qqmapsdk = new QQMapWX({
      key: "P56BZ-PYZ66-VLOSC-ER37Q-KSVO2-CTBD5"
    })
    wx.getLocation({
      type: 'wgs84',
      success: function (res) {
        qqmapsdk.reverseGeocoder({
          location: {
            latitude: res.latitude,
            longitude: res.longitude
          },
          success: function (adressRes) {
            let pro = adressRes.result.address_component.province;
            let cityname = adressRes.result.address_component.city;
            that.setData({
              city: cityname,
              cueflag: true,
              citypro: pro
            })
          },
          fail: function (res) {
            that.setData({
              cuemsg: "请在网络良好的时候重试或直接手动选择城市"
            })
          },
        })
      },
      fail: function (res) {
        console.log(res);
        console.log("失败")
        that.setData({
          cuemsg: "请检查位置权限或直接手动选择城市"
        })
      }
    })
  },

  onLoad: function (options) {
    if (options.payfor && options.orderid) {
      this.setData({
        payfor: true,
        orderid: options.orderid
      })
    }
    wx.showLoading({
      title: '等待中',
    })
    wx.getNetworkType({
      success: function (res) {
        if (res.networkType == "none") {
          wx.showModal({
            title: '温馨提示',
            content: '当前网络不可用，请检查您的网络！',
            showCancel: false
          })
        }
      },
    })
    let that = this;
    this.position();
    app.request("area", {}, true).then(res => {
      let words = [];
      let wordsList = [];
      for (let i in res.data) {
        words.push(i);
        for (let a in res.data[i]) {
          let aa = res.data[i][a];
          aa.open = false;
        }
      }
      this.setData({
        words: words,
        cityList: res.data,
        loading: false

      })
    })
    wx.hideLoading()
    try {
      var res = wx.getSystemInfoSync()
      this.pixelRatio = res.pixelRatio;
      this.apHeight = 16;
      this.offsetTop = 80;
      let height = res.windowHeight;
      this.setData({ windowHeight: height })
    } catch (e) {

    }

  },
  choiceWordindex: function (e) {
    let that = this;
    let wordindex = e.currentTarget.dataset.wordindex;
    this.setData({
      toView: wordindex,
      card: wordindex,
      cardflag: true
    })
    var tiem = setTimeout(function () {
      that.setData({
        cardflag: false
      })
    }, 500)
  },
  handlerAlphaTap(e) {
    let ap = e.target.dataset.wordindex;
    this.setData({ toView: ap });
  },
  handlerMove(e) {
    let wordindex = this.data.words;
    let moveY = e.touches[0].clientY;
    let rY = moveY - this.offsetTop;
    if (rY >= 0) {
      let index = Math.ceil((rY - this.apHeight) / this.apHeight);
      if (0 <= index < wordindex.length) {
        let nonwAp = wordindex[index];
        nonwAp && this.setData({ toView: nonwAp });
      }
    }
  },
  cityopen: function (e) {
    let id = e.currentTarget.dataset.cid;
    let idx = e.currentTarget.dataset.cletter;
    let list = this.data.cityList;
    for (let i in list) {
      for (let j in list[i]) {
        let a = list[i][j];
        if (a.id == id) {
          a.open = !a.open
        } else {
          a.open = false
        }
      }
    }
    this.setData({
      cityList: list
    })
  },
  // 点击城市的时候
  changenewcity: function (e) {

    let areacode = e.currentTarget.dataset.areacode;
    let areaname = e.currentTarget.dataset.cname;
    let index = e.currentTarget.dataset.idw;
    let obj = {
      areacode: areacode,
      index: index
    }
    this.setData({
      newplace: areaname,
      cityFlag: true,
      allMsg: obj
    })
  },

  //点击定位跳转
  positioncity: function (e) {
    let city = e.currentTarget.dataset.bcity;
    let citypro = e.currentTarget.dataset.citypro;
    let a = city.replace("市", "");
    let b = a.replace("县", "");
    let obj = {
      province: citypro,
      city: b
    }
    this.setData({
      newplace: city,
      cityFlag: true,
      allMsg: obj
    })
  },

  //点击城市跳转
  btncity: function (e) {
    let code = e.currentTarget.dataset.bcity;//code
    let citys = e.currentTarget.dataset.city;
    let obj = {
      areacode: code,
      index: 0
    }
    this.setData({
      newplace: citys,
      cityFlag: true,
      allMsg: obj
    })
  },

  //留下城市名字
  addcity: function (e) {
    this.setData({
      flag: true,
      addbtn: true,
      changecolor: '#b7b4b4'
    })
  },
  // 获取焦点back
  selectCity: function () {
    wx.setNavigationBarTitle({
      title: '查找公积金中心',
    })
    this.setData({
      flag: true,
      scrollflag: false
    })
  },
  //绑定输入
  selectcom: function (e) {
    let a = e.detail.value.replace(/\s+/g, '');
    let that = this;
    this.setData({
      content: e.detail.value.replace(/\s+/g, ''),
    })
    console.log(this.data.content);
    console.log(e.detail.value)
    if (this.data.content != "") {
      app.request("area/area_name", { area_name: this.data.content }, true).then(res => {
        if (res.code == 200) {
          this.setData({
            listviewdata: res.data
          })
          if (res.data.length == 0) {
            console.log(res.data.length)
            that.setData({
              hint: true,
            })
          } else {
            that.setData({
              hint: false,
              addbtn: false
            })
          }
        }
      })
    } else {
      that.setData({
        listviewdata: null,
        hint: false
      })
      if (that.data.listviewdata == null || that.data.listviewdata.length == 0 || that.data.content == "") {
        that.setData({
          hint: false
        })
      }
    }
  },
  //点击查询列表进行跳转
  listviewbtn: function (e) {
    let areacode = e.currentTarget.dataset.listcode;
    let areaname = e.currentTarget.dataset.cname;
    let obj = {
      areacode: areacode
    }
    this.setData({
      newplace: areaname,
      cityFlag: true,
      allMsg: obj
    })
    wx.setStorageSync("allMsg", obj)
  },
  //取消查询
  cancel: function (e) {
    this.setData({
      flag: false,
      scrollflag: true,
      content: "",
      hint: false,
      addbtn: false,
      changecolor: "#1a96ff",
      icon: false,
      newcity: ""
    })
    wx.setNavigationBarTitle({
      title: '城市选择',
    })
  },
  // 提交城市
  confirmcity: function (e) {
    let that = this;
    let b = e.detail.value;
    app.request("leave_message", { city_name: that.data.newcity }, true).then(res => {
      if (res.code == 200) {
        wx.showToast({
          title: '成功',
          icon: 'succes',
          duration: 1000,
          mask: true
        })
        that.setData({
          newcity: "",
          icon: false
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  changefineshed: function (e) {
    let city = e.detail.value.replace(/\s+/g, '');
    this.setData({
      newcity: e.detail.value.replace(/\s+/g, '')
    })
    if (city !== "") {
      this.setData({
        icon: true
      })
    } else {
      this.setData({
        icon: false
      })
    }
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (this.data.listviewdata == null || this.data.listviewdata.length == 0) {
      this.setData({
        hint: false
      })
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: "我正在查询公积金，快来查询吧",
      path: '/pages/index/index?channel=home',
      imageUrl: 'https://head.image.jiajijie.com/dj/images/wuxianyijn.png',
      success: function (res) {
        wx.showShareMenu({
          withShareTicket: true
        })
        console.log("成功")
        console.log(res)
      },
      fail: function (res) {
        console.log("失败")
        console.log(res)
      }
    }
  }
})